<?php
namespace Api\Controller;
use Think\Controller;
class ImController extends Controller {

	public function _initialize(){
       $identifier='sysadmin';
       $expiry_after='3600';
       $protected_key_path='/www/wwwroot/www_jtcwys_cn/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/private_key';
       $tool_path='/www/wwwroot/www_jtcwys_cn/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/signature';
       vendor('PhpImServerSdk/TimRestApi');
       $this->TimRestApi=  new \Vendor\PhpImServerSdk\TimRestAPI();
       $this->TimRestApi->generate_user_sig($identifier,$expiry_after,$protected_key_path,$tool_path);
    }

      public function getUserSig(){
           $uid=I('uid');
           $info=$this->toUidCreatSig($uid);
           EchoData($info);
  }

    public function toUidCreatSig($uid){
    	    $identifier=$uid;
    	    $sdkappid='1400133121';
    	    $signature='/www/wwwroot/www_jtcwys_cn/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/signature';
    	    $private_key_path='/www/wwwroot/www_jtcwys_cn/ThinkPHP/Library/Vendor/PhpImServerSdk/signature/private_key';
            $command = $signature
          . ' ' . escapeshellarg($private_key_path)
          . ' ' . escapeshellarg($sdkappid)
          . ' ' . escapeshellarg($identifier);
          $ret = exec($command, $out, $status);
          return $out[0];
    }
//获取登录信息
  public function getLoginInfo(){
  	$uid=I('uid');
  	$data['sdkAppID']='1400133121';
  	$data['appIDAt3rd']='1400133121';
  	$data['identifier']=$uid;
  	$data['identifierNick']='';
  	$data['accountType']='36669';
  	$data['userSig'] =$this->toUidCreatSig($uid);
  	EchoData($data);
  }

//获取用户信息
  public function getUserInfo(){
  	     $where['id']=I('id');
         $info=fd('User',$where,'head');
         $info['head']=C('HOST_URL').$info['head'];
         if($info){
            EchoData($info);
         }else{
         	EchoData($info,0,101,'没有此用户');
         }
  }
  //获取医生信息
  public function getDoctorInfo(){
  	     $where['id']=I('did');
         $info=fd('Doctor',$where,'head');
         $info['head']=C('HOST_URL').$info['head'];
         if($info){
            EchoData($info);
         }else{
         	EchoData($info,0,101,'没有此医生用户');
         }
  }
  //创建用户
   public function addUser(){
        $uinfo='doctor10';
        echo $uinfo;
        $info=$this->TimRestApi->account_import($identifier=$uinfo,$nick=$uinfo,$face_url='http://www.cwlyz.cn/Public/img/logo.png');
         dump($info);
      }
 
  //用户退出设置
      public function Userout(){
        $json='{
    "CallbackCommand": "State.StateChange",
    "Info": {
        "Action": "Logout",
        "To_Account": "1",
        "Reason": "Unregister"
    }
}';
  $url="www.example.com?SdkAppid=1400133121&CallbackCommand=State.StateChange&contenttype=json";
  $info=$this->http_req('https','post',$url,$json);


  dump($info);
      }


      public  function http_req($http_type, $method, $url, $data)
  {
    $ch = curl_init();
    if (strstr($http_type, 'https'))
    {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    }   

    if ($method == 'post')
    {
      curl_setopt($ch, CURLOPT_POST, 1); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    } else
    {
      $url = $url . '?' . $data;
    }   
  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT,100000);//超时时间

    try
    {
      $ret=curl_exec($ch);
    }catch(Exception $e)
    {
      curl_close($ch);
      return json_encode(array('ret'=>0,'msg'=>'failure'));
    }
    curl_close($ch);
    return $ret;
  }
   
   //获取COS 配置项
    function config(){
        $data['Bucket']='im-1252230039';
        $data['Region']='ap-chengdu';
        $data['SecretId']='AKID2uYx73uESRCm8K62Xopa5KJ3Ynshrnvt';
        $data['SecretKey']='ynQvBAuOcZGhrkOmumOksmRq1xe9R6bx';
        EchoData($data);
    }  

    //添加评论
    function AddComment(){
      $add['ct_content']=I('content');
      $add['ct_uid']=I('uid');
      $add['ct_did']=I('did');
      $add['ct_ctime']=time();
      $add['ct_star']=I('star');
      $info=ad('comment',$add);
      EchoData($info);
    }  

    //小程序模板消息
    function SendTempInfo(){
      $AccessToken=GetAccessToken();
      $url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$AccessToken;
      $data='{
  "touser": "oGU_-0HuODOLuD495EIWrW9UNUPs",
  "template_id": "crbEcm-PA36QBdoKm7YbvQRMyA4KLoBMpszm-GuwJxs",
  "page": "/pages/index/index",
  "form_id": "c66201a0352c45a4928887e337be2c9f",
  "data": {
    "keyword1": {
      "value": "2015年01月05日 12:30"
    },
    "keyword2": {
      "value": "宠物领养小站"
    },
    "keyword3": {
      "value": "腾讯微信总部"
    }
  },
  "emphasis_keyword": "keyword1.DATA"
}';
     $info= curl($url,true,'post',$data);
     dump($info);
    } 


   
}